package com.fei.BYSJ.controller;

import com.fei.BYSJ.beans.Data;
import com.fei.BYSJ.entity.Order;
import com.fei.BYSJ.entity.OrdersTag;
import com.fei.BYSJ.entity.User;
import com.fei.BYSJ.service.OrderService;
import com.fei.BYSJ.service.OrdersTagService;
import com.fei.BYSJ.service.TagService;
import com.fei.BYSJ.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/tag")
public class TagController {
@Autowired
    OrdersTagService ordersTagService;
@Autowired
    TagService tagService;
@Autowired
    OrderService orderService;

    @RequestMapping("/alltagbyorder")
    public @ResponseBody
    Data alltagbyorder(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        return ResultData.success("",ordersTagService.findAllTagByOrderID(id));
    }

@RequestMapping("/alltag")
public @ResponseBody
Data alltag(HttpServletRequest request){
    return ResultData.success("",tagService.findAllTag());
}


    @RequestMapping("/addOrderTag")
    public @ResponseBody
    Data addOrderTag(HttpServletRequest request,HttpSession session ){
        User user  = (User) session.getAttribute("user");
        OrdersTag ordersTag = new OrdersTag();
        ordersTag.setOrderid(Integer.parseInt(request.getParameter("orderid")));
        ordersTag.setTagid(Integer.parseInt(request.getParameter("tagid")));
        Order o  = orderService.findOrderById(Integer.parseInt(request.getParameter("orderid")));
        List<OrdersTag> ordersTags = ordersTagService.findAllTagByOrderID(ordersTag.getOrderid());

        for (OrdersTag ot : ordersTags
             ) {
            if (ot.getTagid().equals(ordersTag.getTagid())&&ot.getOrderid().equals(ordersTag.getOrderid())){
                  return ResultData.failed("订单已存在此标签","");
            }


        }
        int i=0;
        String msg="";
        if (o.getAccpetstatus() == 0){
        if (user.getUsertype() == 0||o.getUserid().equals(user.getUserid().toString())){
             i = ordersTagService.addOrderTag(ordersTag);
        }
        }

        if (i==1){
            return ResultData.success("添加成功","");
        }

        return ResultData.failed("添加失败","");
    }










    }


